//
//  StatusCellPictureView.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/20.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit
import SnapKit
/// 配图视图
private let statusCellPictureViewResuseID = "StatusCellPictureView"
class StatusCellPictureView: UICollectionView {
    //设置layout
    init () {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = StatusCellPictureItemMargin
        layout.minimumInteritemSpacing = StatusCellPictureItemMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        //        注册cell
        registerClass(StatusCellPictureViewCell.self, forCellWithReuseIdentifier: statusCellPictureViewResuseID)
        //设置数据源  数据源设置为自己
        dataSource = self
        delegate = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var viewModel: StatusViewModel? {
        didSet {
            sizeToFit()
            //刷新
            reloadData()
        }
    }
    override func sizeThatFits(size: CGSize) -> CGSize {
        return cellHeigth()
    }
    //计算配图的高度  设置配图的样式
    func cellHeigth() -> CGSize {
        /**
        
        先设置item的大小，再根据item的大小设置宽高
        */
        let rowCount: CGFloat = 3
        let totalWidth = UIScreen.mainScreen().bounds.width - 2 *   StatusCellMargin
        
        let itemWidth = (totalWidth - 2 * StatusCellPictureItemMargin) / rowCount
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        //根据返回来的数量 确定配图的总宽度和总高度
        let count = viewModel?.thumbnailURLs?.count ?? 0
        //无图
        if count == 0 {
            return CGSizeZero
        }
        //1张图
        if count == 1 {
            //TODO:暂时蛇者一个size
            let size = CGSizeMake(150, 100)
            
            layout.itemSize = size
            return size
        }
        // 2*2 = 4张图
        if count == 4 {
            let w = itemWidth * 2 + StatusCellPictureItemMargin
            return CGSizeMake(w, w)
        }
        /**
        2 3
        5 6
        7 8 9
        */
        let w = itemWidth * rowCount + (rowCount - 1) * StatusCellPictureItemMargin
        let rowIndex  = CGFloat((count - 1) / Int(rowCount) + 1)
        let h = rowIndex * itemWidth + (rowIndex - 1) * StatusCellPictureItemMargin
        return CGSizeMake(w, h)
    }
    
}
// MARK: - 实现数据源方法 - 代理方法
extension StatusCellPictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.thumbnailURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(statusCellPictureViewResuseID, forIndexPath: indexPath) as! StatusCellPictureViewCell
        cell.imageURL = viewModel!.thumbnailURLs![indexPath.row]
        return cell
    }
    //代理方法
<<<<<<< HEAD
    func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBStatusSelectedPhotoNotification, object: self, userInfo: ["indexPath":indexPath, "urls": viewModel!.thumbnailURLs!])
    }
=======
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBStatusSelectedPhotoNotification, object: self, userInfo: ["indexPath":indexPath, "urls": viewModel!.thumbnailURLs!])
    }
    
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
}

//自定义cell
private class StatusCellPictureViewCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            iconImageView.sd_setImageWithURL(imageURL,
                placeholderImage: nil,
                options:[])
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.redColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        
        contentView.addSubview(iconImageView)
        //设置约束
        //        因为返回的cell大小是不一样的  所以要设置自动布局
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //懒加载
    private var iconImageView: UIImageView = {
        
        let iv = UIImageView()
        //设置图片的填充方式
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        iv.clipsToBounds = true
        
        return iv
        }()
}